# web.explorer模块
from fastapi import APIRouter , HTTPException, status
from model.explorer import Explorer 
from service import explorer as service 
from error import Missing, Duplicate 


router = APIRouter(prefix='/explorer')


@router.get('')
@router.get('/') 
def get_all() -> list[Explorer]: 
    try:
        return service.get_all() 
    except Missing as e: 
        raise HTTPException(status_code=404,detail=e.msg)

@router.get('/{name}') 
def get_one(name) -> Explorer | None: 
    try:
        return service.get_one(name) 
    except Missing as e: 
        raise HTTPException(status_code= status.HTTP_404_NOT_FOUND, detail=e.msg)

@router.post('', status_code=201)
@router.post('/', status_code=201) 
def create(explorer: Explorer) -> Explorer: 
    try:
        return service.create(explorer)
    except Duplicate as e: 
        raise HTTPException(status_code=404, detail=e.msg)

@router.patch('/{name}', status_code=201) 
def modify(name: str, explorer: Explorer) -> Explorer: 
    try:
        return service.modify(name, explorer) 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)


@router.delete('/{name}', status_code=status.HTTP_204_NO_CONTENT)
def delete(name: str) -> None: 
    try:
        return service.delete(name) 
    except Missing as e: 
        raise HTTPException(status_code=404, detail=e.msg)
    


